package com.eric.husky.controller;

import com.eric.husky.common.web.ResponseModel;
import com.eric.husky.service.FileService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @program: husky
 * @description:
 * @author: eric
 * @create: 2021-03-15 14:08
 **/
//上传不要用@Controller,用@RestController
@RestController
@RequestMapping("/file")
public class FileController {
    //在文件操作中，不用/或者\最好，推荐使用File.separator
    @Autowired
    FileService fileService;

    @ApiOperation(value = "上传接口")
//    @ApiImplicitParam(name = "file", value = "文件", paramType = "form", required = true, dataType = "MultipartFile")
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file)
            throws IllegalStateException, IOException {
        return fileService.upload(file);
    }

    @ApiOperation(value = "下载接口")
    @ApiImplicitParam(name = "downloadFilePath", value = "文件地址", paramType = "query", required = true)
    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam String downloadFilePath) {
        fileService.download(response, downloadFilePath);
    }
}

